package com.intercepter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.common.EmiJsonObj;
import com.common.ResultData;
import com.common.WebUtils;

public class IntercepterAdmin extends HandlerInterceptorAdapter {
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
	
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		boolean bo = true;
        if(handler.getClass().isAssignableFrom(HandlerMethod.class)){
        	HandlerMethod hm = (HandlerMethod)handler;
        	
        	//AuthPassport authPassport = hm.getBeanType().getAnnotation(AuthPassport.class);
        	//if(authPassport==null)
        	AuthPassport authPassport = hm.getMethodAnnotation(AuthPassport.class);
        	if(authPassport!=null){
        		ServletContext sc = request.getSession().getServletContext();
        		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
        		/*FunServiceImpl funServiceImpl = (FunServiceImpl)context.getBean("funServiceImpl");
        		
        		UserBean user = (UserBean) request.getSession().getAttribute("user");
        		//获取用户权限
        		List<Map> listMenu = funServiceImpl.getAll(user.getId());
        		
        		String actionName = authPassport.actionName().toLowerCase();
        		String controllerName = authPassport.controllerName().toLowerCase();
        		String path = request.getContextPath();
        		String url = String.format("/%s/%s", controllerName,actionName);
        		boolean hasRight = false;
        		for(Map m : listMenu) {
        			if(m.get("linkurl").toString().contains(url)) {
        				hasRight = true;
        				break;
        			}
        		}*/
        		boolean hasRight = true;
        		if(!hasRight){
        			if(WebUtils.isAjaxRequest(request)){
        				ResultData<String> rd = new ResultData<>();
        				rd.setType(3);
        				rd.setMessage("对不起，您没有操作权限!");
        				//String jsonString = JSON.toJSONString(rd);
        				request.setCharacterEncoding("utf-8");
        				response.setContentType("application/json;charset=utf-8");
                		response.getWriter().print(EmiJsonObj.fromObject(rd).toString());
                	}
        			else{
            			//response.sendRedirect(path+"/login/notfunction.do");
                	}
        			bo = false;
        		}
        	}
        }
        
        return bo;
	}
}
